home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1996 October / MACPOWER-1996-10.ISO.7z / MACPOWER-1996-10.ISO / AMUG / Internet_31 / Analog 1.9b6d / Analog 1.9b6d ト / newlog.c < prev    next >
C/C++ Source or Header  |  1996-06-06  |  6KB  |  175 lines

  1. /* NEWLOG.C - Version 1.1 4/23/96
  2.  
  3.     Copyright (c) 1996 by Jason T. Linhart
  4.     This file is freeware. You may use it and give copies to friends, so
  5.     long as you include all of the file without modification. It may not
  6.     be sold or commercially distributed without a written licence from me.
  7.     It may be included in archives, and distributed on CD-ROM or on other
  8.     formats so long as there are no charges for these services other than
  9.     shipping, handling and the cost of media. Use or distribution of this
  10.     file indicates your agreement to these terms.
  11.  
  12.     Created 1/17/96 by Jason T. Linhart to replace Netscape default logger
  13.     Modeled after 'nosy.c' Netscape example of additional logging info.
  14.     Updated 2/7/96 by Jason T. Linhart to provide NCSA combined log format.
  15. */
  16.  
  17. /*
  18.     This NSAPI module will cause a Netscape server to produce an NCSA combined
  19.     format log file instead of the NCSA common format log it normaly creates.
  20.     This is only useful with Netscape servers, and requires various headers
  21.     supplied by Netscape. You will have to figure out how to compile and
  22.     link this file yourself based on the Netscape documentation. Then:
  23.  
  24.     1. Append the following lines to your magnus.conf file:
  25.  
  26.          Init fn=load-modules shlib=<your fullpath>/logger.so funcs=exlog-init,exlog-log
  27.          Init fn=exlog-init file=<your fullpath>/access
  28.  
  29.     2. Edit your "obj.conf" file to comment out the existing AddLog line and
  30.        include the following line in the "<Object name=default>" section:
  31.  
  32.          # AddLog fn=common-log
  33.          AddLog fn=exlog-log
  34.  
  35.     3. Shut down your server, then start it up again (don't restart).
  36.  
  37. These are the fields that are logged:
  38.  
  39. DNS-name, client user name, auth user name, start time, request ( method, URL, protocol ), ¥
  40.     status, bytes, refer, client
  41.  
  42. This is exactly the same as the common log format except that refer and client are added
  43. at the end. Your "access" log file should look like this:
  44.  
  45. cronos.mcom.com - - [11/Aug/1995:23:48:05 -0700] "GET / HTTP/1.0" 304 - "http://coltrane.mcom.com:1995/https-1967/bin/restart?" "Mozilla/1.1N (Macintosh; I; 68K)"
  46.  
  47. */
  48.  
  49. /* standard headers for SAFs */
  50.  
  51. #include "base/pblock.h"
  52. #include "base/session.h"
  53. #include "frame/req.h"
  54.  
  55. /* specific headers useful for logging functions */
  56.  
  57. #include "base/util.h"       /* is_mozilla, getline */
  58. #include "frame/protocol.h"  /* protocol_start_response */
  59. #include "base/file.h"       /* system_fopenRO */
  60. #include "base/buffer.h"     /* filebuf */
  61. #include "frame/log.h"       /* log_error */
  62.  
  63. #include <stdio.h>
  64. #include <unistd.h>
  65. #include <time.h>
  66.  
  67. /* File descriptor to be shared between the processes */
  68.  
  69. static SYS_FILE logfd = SYS_ERROR_FD;
  70.  
  71. /* function to shut down the logging; will be called on server restart */
  72.  
  73. void
  74. exlog_term(void *parameter)
  75. {
  76.     system_fclose(logfd);
  77.     logfd = SYS_ERROR_FD;
  78.     }
  79.  
  80. /* init function for logging */
  81.  
  82. int
  83. exlog_init(pblock *pb, Session *sn, Request *rq)
  84. {
  85.     /* file parameter */
  86.     char *fn = pblock_findval("file", pb);
  87.     
  88.     if (!fn) {
  89.         pblock_nvinsert("error", "Exlog-init: please supply a file name", pb);
  90.         return REQ_ABORTED;
  91.         }
  92.     logfd = system_fopenWA(fn);
  93.     if (logfd == SYS_ERROR_FD) {
  94.         pblock_nvinsert("error", "Exlog-init: unable to open file", pb);
  95.         return REQ_ABORTED;
  96.         }
  97.     /* Close log file when server is restarted */
  98.     magnus_atrestart(exlog_term, NULL);
  99.     return(REQ_PROCEED);
  100.     }
  101.  
  102. /* actual logging function! */
  103.  
  104. int
  105. exlog_log(pblock *pb, Session *sn, Request *rq)
  106. {
  107.     /* working variables */
  108.     struct tm date;
  109.     time_t timer;
  110.     char *req_str, *dns, *uname, time_str[32+10], status[6], *length;
  111.     char *from, *agent, *referer, *empty="-¥0";
  112.     char *logmsg, *temp;
  113.     long offset;
  114.     int len;
  115.  
  116.     /*
  117.         Stuff we want to log:
  118.             DNS-name, client user name, auth user name, start time, 
  119.             request ( method, URL, protocol ), status, bytes, client, refer
  120.      */
  121.     dns = session_dns(sn);
  122.     if (!dns)                                                 /* use IP if no DNS found */
  123.         dns = pblock_findval("ip", sn->client);
  124.     from = pblock_findval("from", rq->headers);
  125.     uname = pblock_findval("auth-user", rq->vars);
  126.     timer=time((void *)0);
  127.     date = *localtime(&timer);
  128.     if (strftime(time_str,32,"[%d/%b/%Y:%H:%M:%S ",&date)) {
  129.         if (daylight && date.tm_isdst>0) 
  130.             offset=altzone;
  131.         else offset=timezone;
  132.         offset/=60;
  133.         if (offset<0) 
  134.             offset = -offset;
  135.         else strcat(time_str,empty);
  136.         sprintf(time_str+strlen(time_str),"%02d%02d]",offset/60,offset%60);
  137.         }
  138.     else strcpy(time_str,empty);
  139.     req_str = pblock_findval("clf-request", rq->reqpb);
  140.     temp = pblock_findval("status", rq->srvhdrs);
  141.     if (temp && *temp) {
  142.         status[0]=temp[0];
  143.         status[1]=temp[1];
  144.         status[2]=temp[2];
  145.         status[3]=0;
  146.         }
  147.     else strcpy(status,empty);
  148.     length = pblock_findval("content-length", rq->srvhdrs);
  149.     agent = pblock_findval("user-agent", rq->headers);
  150.     referer = pblock_findval("referer", rq->headers);
  151.  
  152.     /* make NULL results be a dash */
  153.     
  154.     if (!dns || !*dns) dns=empty;
  155.     if (!from || !*from) from=empty;
  156.     if (!uname || !*uname) uname=empty;
  157.     if (!req_str || !*req_str) req_str=empty;
  158.     if (!length || !*length) length=empty;
  159.     if (!agent) agent="";
  160.     if (!referer) referer="";
  161.     
  162.     /* print it! cronos.mcom.com - - [11/Aug/1995:23:48:05 -0700] "GET / HTTP/1.0" 304 - */
  163.     logmsg=(char *)MALLOC(strlen(dns)+strlen(from)+strlen(uname)+strlen(time_str)+strlen(req_str)+
  164.                             strlen(status)+strlen(length)+strlen(agent)+strlen(referer)+18);
  165.     len=util_sprintf(logmsg, "%s %s %s %s ¥"%s¥" %s %s ¥"%s¥" ¥"%s¥"¥n", dns, from, uname,
  166.         time_str, req_str, status, length, referer, agent);
  167.  
  168.     system_fwrite_atomic(logfd, logmsg, len);
  169.     FREE(logmsg);
  170.  
  171.     return(REQ_PROCEED);
  172.     }
  173.  
  174. /* END OF NEWLOG.C */
  175.